package com.edgar.plugin;

import com.edgar.plugin.utils.ClassUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Edgar on 2018/2/1.
 */
public final class SystemClassHolder {

    private static final String ACTIVITY_MANAGER_CLASS = "android.app.IActivityManager";
    private static final String PACKAGE_MANAGER_CLASS = "android.content.pm.IPackageManager";
    private static final String ACTIVITY_THREAD_CLASS = "android.app.ActivityThread";
    private static final String CONTEXT_IMPL_CLASS = "android.content.ContextImpl";
    private static final String BASE_DEX_CLASSLOADER_CLASS = "dalvik.system.BaseDexClassLoader";
    private static final Map<String,Class<?>> SYSTEM_CLASS_MAP = new HashMap<>();

    private SystemClassHolder(){}

    public static synchronized Class<?> get(String className){
        Class<?> clazz = SYSTEM_CLASS_MAP.get(className);
        if (clazz == null){
            clazz = ClassUtils.loadClass(className);
            SYSTEM_CLASS_MAP.put(className,clazz);
        }
        return clazz;
    }

    public static Class<?> activityManagerClass(){
        return get(ACTIVITY_MANAGER_CLASS);
    }

    public static Class<?> activityThreadClass(){
        return get(ACTIVITY_THREAD_CLASS);
    }

    public static Class<?> baseDexClassLoaderClass(){
        return get(BASE_DEX_CLASSLOADER_CLASS);
    }

    public static Class<?> contextImplClass(){
        return get(CONTEXT_IMPL_CLASS);
    }

    public static Class<?> packageManagerClass(){
        return get(PACKAGE_MANAGER_CLASS);
    }
}